/*******************************************************************************
																				
	DESCRIPTION:  	This do file analyses the ensemble predictions generated 
					using a model from a fixed year for individuals from all years. 
	
*******************************************************************************/

clear all
global id_code 128

* Import programs:
run  "${code}/Output Generation/108_0_MainStatistics_Program.do"

pause on
set seed 2110

/*******************************************************************************
*	1. Predicted job-finding rates - time series - predictions done by year
********************************************************************************/
local model Full
local year 2006
local model_sample Full
local yearStart 1992
local yearEnd 2016
local yearSpan 1993/2016

* Combine the predictions made for each year
use "${data}/003_MainWithEnsemblePred_`model_sample'_`yearStart'Individuals_TrainedOn`year'modelIndividuals_`model'.dta", clear

forval year_sample = `yearSpan' {
	append using "${data}/003_MainWithEnsemblePred_`model_sample'_`year_sample'Individuals_TrainedOn`year'modelIndividuals_`model'.dta"
}

compress

* Create frame to store results:
frame create stats str30(outcome) year0 n_obs ymean ymean_p yvar yvar_p cov r_sq

* Calculate summary statistics for each year:  	
forval year_sample = 1992/2016 {
	
	foreach outcome in emplAft6M_0M_In emplAft6M_6M_In emplAft6M_12M_In {
		
		stats `outcome' p_`outcome' if year == `year_sample'
		frame post stats ("`outcome'") (`year_sample') (`r(n_ex)') (`r(mean_f_ex)') (`r(mean_f_hat_ex)') (`r(var_f_ex)') (`r(var_f_hat_ex)') (`r(cov_ex)') (`r(r2_ex)')
	}
}

frame stats: save "${data}/${id_code}_Statistics_`model_sample'_`yearStart'_`yearEnd'Individuals_TrainedOn_`model'`year'.dta", replace

* Do the same for yearly models:
local model Full
local year 2006
local model_sample Full
local yearStart 1992
local yearEnd 2016
local yearSpan 1993/2016
use "${data}/003_MainWithEnsemblePred_`model'_`yearStart'.dta", clear

forval year = `yearSpan' {
	append using "${data}/003_MainWithEnsemblePred_`model'_`year'.dta"
}

compress

* Create frame to store results:
cap frame drop stats
frame create stats str30(outcome) year0 n_obs ymean ymean_p yvar yvar_p cov r_sq

* Calculate summary statistics for each year:  	
forval year = 1992/2016 {
	
	foreach outcome in emplAft6M_0M_In emplAft6M_6M_In emplAft6M_12M_In {
		
		stats `outcome' p_`outcome' if year == `year'
		frame post stats ("`outcome'") (`year') (`r(n_ex)') (`r(mean_f_ex)') (`r(mean_f_hat_ex)') (`r(var_f_ex)') (`r(var_f_hat_ex)') (`r(cov_ex)') (`r(r2_ex)')
	}
}

frame stats: save "${data}/${id_code}_Statistics_`model'_`yearStart'_`yearEnd'.dta", replace
